package com.community.contents.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.community.common.core.constant.RedisKey;
import com.community.common.core.exception.CommunityException;
import com.community.common.core.utils.Result;
import com.community.common.security.dto.UserLoginDto;
import com.community.contents.dto.TopicDTO;
import com.community.contents.dto.TopicUserDTO;
import com.community.contents.entity.PostEntity;
import com.community.contents.entity.TopicEntity;
import com.community.contents.entity.TopicUserEntity;
import com.community.contents.entity.UserEntity;
import com.community.contents.service.ITopicService;
import com.community.contents.service.ITopicUserService;
import com.community.contents.vo.TopicVo;
import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException;
import io.swagger.annotations.Api;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.object.SqlQuery;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Type;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 圈子
 *
 * @author hjs hjs@qq.com
 * @since 1.0.0 2023-02-21
 */
@RestController
@RequestMapping("contents/topic")
@Api(tags = "圈子")
public class TopicController {
    @Autowired
    private ITopicService topicService;
    @Autowired
    private ITopicUserService topicUserService;
    @Autowired
    private RedisTemplate redisTemplate;


    @PostMapping("page")
    public Result<Page<TopicDTO>> page(@RequestBody Page<TopicDTO> params) {
        topicService.pageDto(params);
        return new Result<Page<TopicDTO>>().ok(params);
    }

    //添加圈子--view
    @PostMapping("save")
    @Transactional(rollbackFor = Exception.class, timeout = 6000)
    public Result save(@RequestBody TopicEntity dto) {

        //判断该类目下的圈子名称是否已存在
        QueryWrapper<TopicEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", dto.getName());
        TopicEntity one = topicService.getOne(queryWrapper);
        if (one != null) {
            throw new CommunityException("该圈子已存在，请重新命名");
        }
        dto.setCreateTime(new Date());
        //添加圈子
        boolean save = topicService.save(dto);
        //圈主默认加入圈子
        topicUserService.joinTopic(dto.getId(), dto.getUserId());
        return new Result<>().ViewOk(dto.getId());

    }

    //圈子详情页跳转--view
    @GetMapping("detail")
    public Result detail(Integer id, Integer userId) {

        TopicVo detail = topicService.detail(id);
        if (userId != null) {//判断用户是否登录
            if (topicUserService.isJoin(id, userId)) {//判断用户是否加入了圈子
                detail.setJoin(true);
            }
        }
        return new Result<>().ViewOk(detail);
    }


    //用户加入圈子--view
    @PostMapping("joinTopic")
    public Result joinTopic(@RequestBody Map<String, Object> id, UserLoginDto userLoginDto) {
        //先判断是否已加入过圈子
        int id1 = Integer.parseInt((String) id.get("id"));
        if (topicUserService.isJoin(id1, userLoginDto.getUserId())) return new Result().ViewOk(null);
        //用户加入圈子
        boolean save = topicUserService.joinTopic(id1, userLoginDto.getUserId());
        return new Result().out(save);
    }

    //获取当前用户加入的圈子
    @GetMapping("currentUserJoinTopic")
    public Result currentUserJoinTopic(@RequestParam("page") Integer current, UserLoginDto userLoginDto) {
        Page<TopicEntity> page = new Page<>();
        page.setCurrent(current);
        QueryWrapper<TopicEntity> queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", userLoginDto.getUserId());
        topicService.page(page, queryWrapper);
        return new Result().ViewOk(page);
    }


    /**
     * <p>
     *     我的頁面--》我的圈子方法
     * </p>
     *
     * @param userLoginDto
     * @return
     * @auth  lwl
     * @since 23-03-03
     */
    @GetMapping("myCreateTopic")
    public Result myCreateTopic(@RequestParam("page") Integer current,UserLoginDto userLoginDto) {
        /*获取用户id*/
        Page<TopicEntity> page = new Page<>();
        page.setCurrent(current);
        QueryWrapper<TopicEntity> queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", userLoginDto.getUserId());
        topicService.page(page, queryWrapper);
        return new Result().ViewOk(page);
    }


    /**
     * 从redis中获取前十的热门圈子
     *
     * @return
     */
    @GetMapping("/hotTopic")
    public Result HotTopic() {
       List<TopicEntity> list = (List<TopicEntity>) redisTemplate.opsForValue().get(RedisKey.HOT_TOPIC);
        return new Result().ViewOk(list);
    }


    @GetMapping("list")
    public Result list(Integer classId,Integer page){
        Page<TopicEntity> page1 = new Page<>();
        page1.setCurrent(page);
        QueryWrapper<TopicEntity> queryWrapper = new QueryWrapper();
        queryWrapper.eq("class_id",classId);
        topicService.page(page1,queryWrapper);

        return new Result().ViewOk(page1);
    }



    /**
     * userList
     *
     * @Description: 查询圈友的列表
     * @Date: 2023/3/7 17:02
     * @Author: hjs
     * @return: com.community.common.core.utils.Result
     **/
    @GetMapping("userList")
    public Result userList(@RequestParam("id") Integer topicId, Page<UserEntity> page) {
        topicService.userList(topicId, page);
        return new Result().ViewOk(page);
    }




}
